import Vue from "vue";

//配置路由相关信息
import VueRouter from "vue-router";

import store from "../store/index";
import PwdChange from "../views/admin/PwdChange";
import adminLogin from "../views/admin/Login";
import LogoutUser from "../views/admin/LogoutUser";
import CompanyList from "@/views/company/CompanyList";


//1.通过Vue.use(插件),安装插件
Vue.use(VueRouter);

//2.创建路由对象
const routes = [

    {
        path: "",
        redirect: "/login",
    },
    {
        path: '/loading',
        component: () => import("../components/common/Loading")

    },
    {
        path: "/admin",
        redirect: "/admin/dashBoard",
    },
    {
        path: "/admin",
        component: () => import("../views/Admin"),
        meta: {
            title: "首页"
        },
        children: [
            {
                path: 'dashBoard',
                name: 'dashBoard',
                component: () => import("../views/admin/DashBoard"),
                meta: {
                    title: '统计',
                    requireAuth: true
                },
            },
            {
                path: 'adminList',
                component: () => import("../views/admin/AdminList"),
                meta: {
                    title: '管理员列表',
                    requireAuth: true
                },
            },
            {
                path: 'userList',
                component: () => import("../views/admin/UserList"),
                meta: {
                    title: '普通用户列表',
                    requireAuth: true
                },
            },
            {
                path: 'hrList',
                component: () => import("../views/admin/HRList"),
                meta: {
                    title: '企业用户列表',
                    requireAuth: true
                },
            },
            {
                path: 'companyList',
                component: () => import("../views/company/CompanyList"),
                meta: {
                    title: '企业列表',
                    requireAuth: true
                },
            },
            {
                path: 'jobList',
                component: () => import("../views/company/JobList"),
                meta: {
                    title: '职位列表',
                    requireAuth: true
                },
            },
            {
                path: 'videoList',
                component: () => import("../views/company/VideoList"),
                meta: {
                    title: '宣讲视频列表',
                    requireAuth: true
                },
            },
            {
                path: 'liveList',
                component: () => import("../views/company/LiveList"),
                meta: {
                    title: '宣讲直播列表',
                    requireAuth: true
                },
            },
            {
                path: 'personInfo',
                component: () => import("../views/admin/PersonInfo"),
                meta: {
                    title: '个人信息',
                    requireAuth: true
                },
            },
            {
                path: "companyList",
                component: CompanyList,
                meta: {
                    title: "企业列表",
                    requireAuth: true,
                },
            },
            {
                path: "pwdChange",
                component: PwdChange,
                meta: {
                    title: "修改密码",
                    requireAuth: true,
                },
            },
            {
                path: "logoutUser",
                component: LogoutUser,
                meta: {
                    title: "注销账号",
                    requireAuth: true,
                },
            },
            {
                path: 'articleManage',
                component: () => import("../views/forum/ArticleManage"),
                meta: {
                    title: '文章管理',
                    requireAuth: true
                },
            },
            {
                path: 'commentManage',
                component: () => import("../views/forum/CommentManage"),
                meta: {
                    title: '评论管理',
                    requireAuth: true
                },
            },
            {
                path: 'companyAudit',
                component: () => import("../views/audit/CompanyAudit"),
                meta: {
                    title: '企业注册审核',
                    requireAuth: true
                },
            },
            {
                path: 'companyUserAudit',
                component: () => import("../views/audit/CompanyUserAudit"),
                meta: {
                    title: '企业用户认证审核',
                    requireAuth: true
                },
            },
            {
                path: 'companyUserAudit',
                component: () => import("../views/audit/CompanyUserAudit"),
                meta: {
                    title: '企业用户认证审核',
                    requireAuth: true
                },
            },
            {
                path: 'articleAudit',
                component: () => import("../views/audit/ArticleAudit"),
                meta: {
                    title: '文章审核',
                    requireAuth: true
                },
            },
            {
                path: 'videoAudit',
                component: () => import("../views/audit/VideoAudit"),
                meta: {
                    title: '视频审核',
                    requireAuth: true
                },
            },
            {
                path: 'newsManage',
                name: 'newsManage',
                component: () => import("../views/web/NewsManage"),
                meta: {
                    title: '资讯公告管理',
                    requireAuth: true
                },
            },
            {
                path: 'courseManage',
                component: () => import("../views/web/CourseManage"),
                meta: {
                    title: '学习课程管理',
                    requireAuth: true
                },
            },
            {
                path: 'resumeTemplateManage',
                name: 'resumeTemplateManage',
                component: () => import("../views/web/ResumeTemplateManage"),
                meta: {
                    title: '简历模板管理',
                    requireAuth: true
                },
            },
            {
                path: 'editNews',
                name: 'editNews',
                component: () => import("@/components/content/MarkDown"),
                meta: {
                    title: '资讯公告编辑',
                    requireAuth: true
                },
            },
            {
                path: 'addResumeTemplate',
                name: 'addResumeTemplate',
                component: () => import("@/components/content/AddRT"),
                meta: {
                    title: '添加简历模板',
                    requireAuth: true
                },
            },

        ]
    },
    {
        path: "/login",
        name: 'login',
        component: adminLogin,
        meta: {
            title: "后台登陆",
        },
    },
];

const router = new VueRouter({
    //配置路由和组件之间的关系
    routes,
    // mode: "history",//history打包回出问题，需要配置化URL
    linkActiveClass: "active",
});

// 前置守卫(guard)
router.beforeEach((to, from, next) => {
  if (to.matched.some((r) => r.meta.requireAuth)) {
    //这里的requireAuth为路由中定义的 meta:{requireAuth:true}，意思为：该路由添加该字段，表示进入该路由需要登陆的
    if (store.getters.token) {
      next();
      let matched=to.matched
      let list=[];
      matched.forEach(element => {
        list.push({"title":element.meta.title,"path": element.path})
      });
      store.dispatch("user/setBreads",list)
    } else {
      next({
        path: "/login",
      });
    }
  } else {
    next();
  }
});


//后置钩子(hook)
router.afterEach((route) => {
    console.log(route.fullPath)
});

//解决两次访问相同路由报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};

// 3.将router对象传到vue实例
export default router;

